﻿using System.IO;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using DG.Tweening;

public enum TJobType {

	Warrior  = 1 , Warlock = 2 , Rouge = 3

}

public class MainController : MonoBehaviour {

	RawImage avatarSnapshotImage;


	void Start () {

		InitFadeOutSence ();
		InitTipPanel ();
		InitJobPanel ();

		// 加载上一场景的截屏
		avatarSnapshotImage = GameObject.Find ("AvatarSnapshotImage").GetComponent<RawImage> ();
		Texture2D image = Resources.Load<Texture2D>("Screen");
		image.Apply ();

		avatarSnapshotImage.material.mainTexture = image;

	}
	

	void Update () { 
		
	}

	void FixedUpdate() {
		
	}

	// -----------------

	void InitFadeOutSence(){
		disableCanvasGroup (GameObject.Find ("EndCanvas"));
	}

	public void FadeOutSence( TweenCallback callback ) {

		GameObject jobCanvas = GameObject.Find ("JobCanvas");
		GameObject endCanvas = GameObject.Find ("EndCanvas");

		enableCanvasGroup ( endCanvas );

		GameObject logo = endCanvas.transform.FindChild ("Logo").gameObject;

		GameObject orilogo = middle.transform.FindChild ("Logo").gameObject;

		logo.GetComponent<RawImage> ().texture = orilogo.GetComponent<RawImage> ().mainTexture;

		Camera camera = jobCanvas.GetComponent<Canvas> ().worldCamera;
		Vector2 pos;

		RectTransformUtility.ScreenPointToLocalPointInRectangle( 
			jobCanvas.GetComponent<RectTransform>() , 
			camera.WorldToScreenPoint( orilogo.transform.position ) , 
			camera , 
			out pos);

		logo.GetComponent<RectTransform> ().anchoredPosition = pos;


		Sequence seq = DOTween.Sequence ();
		seq.OnComplete (callback);
		seq.Append (jobCanvas.GetComponent<CanvasGroup> ().DOFade (0, .5f));

		seq.Append (logo.GetComponent<RectTransform> ().DOScale (new Vector3 (7,7,7), 1.2f).SetEase(Ease.OutExpo));
		seq.Join (endCanvas.GetComponent<CanvasGroup> ().DOFade (0, .8f));

	}


	// -----------------

	GameObject tipPanel;

	void InitTipPanel() {
		tipPanel = GameObject.Find ("TipCanvas");
		CloseTip ();
	}

	public void enableCanvasGroup( GameObject obj ) {
		obj.GetComponent<CanvasGroup> ().alpha = 1;
		obj.GetComponent<CanvasGroup> ().interactable = true;
		obj.GetComponent<CanvasGroup> ().blocksRaycasts = true;
	}

	public void disableCanvasGroup( GameObject obj ) {
		obj.GetComponent<CanvasGroup> ().alpha = 0;
		obj.GetComponent<CanvasGroup> ().interactable = false;
		obj.GetComponent<CanvasGroup> ().blocksRaycasts = false;
	}

	public void ShowTip(TJobType type ) {

		enableCanvasGroup (tipPanel);

		disableCanvasGroup (GameObject.Find ("WarriorTip"));
		disableCanvasGroup (GameObject.Find ("WarlockTip"));
		disableCanvasGroup (GameObject.Find ("RougeTip"));

		switch (type) {
		case TJobType.Warrior:
			enableCanvasGroup (GameObject.Find ("WarriorTip"));
			break;
		case  TJobType.Warlock:
			enableCanvasGroup (GameObject.Find ("WarlockTip"));
			break;
		case TJobType.Rouge:
			enableCanvasGroup (GameObject.Find ("RougeTip"));
			break;
		}

		GameObject.Find ("TipScrollBar").GetComponent<Scrollbar> ().value = 1;
	}

	public void CloseTip(){
		
		disableCanvasGroup (tipPanel);

	}


	// -----------------

	GameObject warriorPanel;
	GameObject warlockPanel;
	GameObject rougePanel;

	GameObject backBg;
	GameObject frontBg;

	GameObject middle;
	GameObject left;
	GameObject right;

	GameObject startButton;

	Vector3 middlePos = new Vector3 (0, 0, 0);
	Vector3 middleRotation = new Vector3 (0, 0, 0);
	Vector3 leftPos = new Vector3(-420,0,150);
	Vector3 leftRotation = new Vector3 (0, 15, 0);
	Vector3 rightPos = new Vector3(420,0,150);
	Vector3 rightRotation = new Vector3 (0, -15, 0);

	Vector3 backLeftPos = new Vector3 (-420, 0, 300);
	Vector3 backLeftRotation = new Vector3 (0, 50, 0);
	Vector3 backRightPos = new Vector3 (420, 0, 300);
	Vector3 backRightRotation = new Vector3 (0, -50, 0);

	Color32 warriorColor = new Color32( 74 , 144 , 226 , 255 );
	Color32 warlockColor = new Color32 (158, 146, 55 , 255 );
	Color32 rougeColor =  new Color32( 24 , 184 , 39 , 255 );

	float duration = 1.8f;

	void InitJobPanel () {

		warriorPanel = GameObject.Find ("Warrior");
		warlockPanel = GameObject.Find ("Warlock");
		rougePanel = GameObject.Find ("Rouge");

		backBg = GameObject.Find ("BackBG");
		frontBg = GameObject.Find ("FrontBG");

		startButton = GameObject.Find ("StartButton");

		middle = warriorPanel;
		right = warlockPanel;
		left = rougePanel;

		backBg.GetComponent<RawImage> ().DOColor( (Color)warriorColor , .01f);
		//frontBg.GetComponent<RawImage> ().DOColor( (Color)warriorColor , .01f);
		startButton.GetComponent<Image> ().DOColor ((Color)warriorColor, .01f);

		middle.GetComponent<RectTransform> ().localPosition = middlePos;
		middle.GetComponent<RectTransform> ().localRotation = Quaternion.Euler (middleRotation);

		left.GetComponent<RectTransform> ().localPosition = leftPos;
		left.GetComponent<RectTransform> ().localRotation = Quaternion.Euler (leftRotation);

		right.GetComponent<RectTransform> ().localPosition = rightPos;
		right.GetComponent<RectTransform> ().localRotation = Quaternion.Euler (rightRotation);

	}

 


	public void Prev( TweenCallback callback ) {

		Sequence seq = DOTween.Sequence ();

		if (right == warriorPanel) {
			seq.Join( backBg.GetComponent<RawImage> ().DOColor ((Color)warriorColor, duration ) );
			//seq.Join( frontBg.GetComponent<RawImage> ().DOColor ((Color)warriorColor, duration ) );
			seq.Join( startButton.GetComponent<Image> ().DOColor ((Color)warriorColor, duration) );
		} else if (right == warlockPanel) {
			seq.Join( backBg.GetComponent<RawImage> ().DOColor ((Color)warlockColor, duration ) );
			//seq.Join( frontBg.GetComponent<RawImage> ().DOColor ((Color)warlockColor, duration ) );
			seq.Join( startButton.GetComponent<Image> ().DOColor ((Color)warlockColor, duration) );
		} else if (right == rougePanel) {
			seq.Join( backBg.GetComponent<RawImage> ().DOColor ((Color)rougeColor, duration ) );
			//seq.Join( frontBg.GetComponent<RawImage> ().DOColor ((Color)rougeColor, duration) );
			seq.Join( startButton.GetComponent<Image> ().DOColor ((Color)rougeColor, duration) );
		}

		SenceProp.backgroundColor = backBg.GetComponent<RawImage> ().color;

		seq.Join (middle.GetComponent<RectTransform> ().DOLocalMove (leftPos, duration , false));
		seq.Join (middle.GetComponent<RectTransform> ().DOLocalRotateQuaternion (Quaternion.Euler (leftRotation), duration));

		seq.Join (right.GetComponent<RectTransform> ().DOLocalMove (middlePos, duration, false));
		seq.Join (right.GetComponent<RectTransform> ().DOLocalRotateQuaternion (Quaternion.Euler (middleRotation), duration));

		Sequence seq2 = DOTween.Sequence ();

		seq2.OnComplete (callback);

		seq2.Append (left.GetComponent<RectTransform> ().DOLocalMove (backLeftPos, duration / 3 , false));
		seq2.Join (left.GetComponent<RectTransform> ().DOLocalRotateQuaternion (Quaternion.Euler (backLeftRotation), duration / 3 ));
		seq2.Join (left.GetComponent<CanvasGroup> ().DOFade (0, duration / 6 ));

		seq2.Append (left.GetComponent<RectTransform> ().DOLocalMove (backRightPos, .001f, false));
		seq2.Join (left.GetComponent<RectTransform> ().DOLocalRotateQuaternion (Quaternion.Euler (backRightRotation), .001f));

		seq2.Append (left.GetComponent<RectTransform> ().DOLocalMove (rightPos, duration / 3 * 2 , false));
		seq2.Join (left.GetComponent<RectTransform> ().DOLocalRotateQuaternion (Quaternion.Euler (rightRotation), duration / 3 * 2 ));
		seq2.Join (left.GetComponent<CanvasGroup> ().DOFade (1, duration / 6 ));
	

		GameObject t;
		t = left;
		left = middle;
		middle = right;
		right = t;

	}

	public void Next(TweenCallback callback) {

		Sequence seq = DOTween.Sequence ();

		if (left == warriorPanel) {
			seq.Join( backBg.GetComponent<RawImage> ().DOColor ((Color)warriorColor, duration ) );
			//seq.Join( frontBg.GetComponent<RawImage> ().DOColor ((Color)warriorColor, duration ) );
			seq.Join( startButton.GetComponent<Image> ().DOColor ((Color)warriorColor, duration) );
		} else if (left == warlockPanel) {
			seq.Join( backBg.GetComponent<RawImage> ().DOColor ((Color)warlockColor, duration ) );
			//seq.Join( frontBg.GetComponent<RawImage> ().DOColor ((Color)warlockColor, duration ) );
			seq.Join( startButton.GetComponent<Image> ().DOColor ((Color)warlockColor, duration) );
		} else if (left == rougePanel) {
			seq.Join( backBg.GetComponent<RawImage> ().DOColor ((Color)rougeColor, duration ) );
			//seq.Join( frontBg.GetComponent<RawImage> ().DOColor ((Color)rougeColor, duration) );
			seq.Join( startButton.GetComponent<Image> ().DOColor ((Color)rougeColor, duration) );
		}

		SenceProp.backgroundColor = backBg.GetComponent<RawImage> ().color;

		seq.Join (middle.GetComponent<RectTransform> ().DOLocalMove (rightPos, duration , false));
		seq.Join (middle.GetComponent<RectTransform> ().DOLocalRotateQuaternion (Quaternion.Euler (rightRotation), duration));

		seq.Join (left.GetComponent<RectTransform> ().DOLocalMove (middlePos, duration, false));
		seq.Join (left.GetComponent<RectTransform> ().DOLocalRotateQuaternion (Quaternion.Euler (middleRotation), duration));

		Sequence seq2 = DOTween.Sequence ();

		seq2.OnComplete (callback);

		seq2.Append (right.GetComponent<RectTransform> ().DOLocalMove (backRightPos, duration / 3 , false));
		seq2.Join (right.GetComponent<RectTransform> ().DOLocalRotateQuaternion (Quaternion.Euler (backRightRotation), duration / 3 ));
		seq2.Join (right.GetComponent<CanvasGroup> ().DOFade (0, duration / 6 ));

		seq2.Append (right.GetComponent<RectTransform> ().DOLocalMove (backLeftPos, .001f, false));
		seq2.Join (right.GetComponent<RectTransform> ().DOLocalRotateQuaternion (Quaternion.Euler (backLeftRotation), .001f));

		seq2.Append (right.GetComponent<RectTransform> ().DOLocalMove (leftPos, duration / 3 * 2 , false));
		seq2.Join (right.GetComponent<RectTransform> ().DOLocalRotateQuaternion (Quaternion.Euler (leftRotation), duration / 3 * 2 ));
		seq2.Join (right.GetComponent<CanvasGroup> ().DOFade (1, duration / 6 ));


		GameObject t;
		t = right;
		right = middle;
		middle = left;
		left = t;

	}


}
